import random

import pandas as pd


def random_select_rows(source_file, target_file, num_rows):
    # 读取源Excel文件
    df = pd.read_excel(source_file)

    # 获取源Excel文件的行数
    num_rows_source = df.shape[0]

    # 随机选择行索引
    selected_rows = random.sample(range(1, num_rows_source), num_rows)
    selected_rows.sort()

    # 根据选择的行索引提取数据
    selected_data = df.iloc[selected_rows]

    # 将数据写入新的CSV文件
    selected_data.to_csv(target_file, index=False)

# 使用示例
source_file = 'data.xls'  # 源Excel文件名
target_file = 'output.csv'  # 目标CSV文件名
num_rows = 30  # 随机选择的行数

random_select_rows(source_file, target_file, num_rows)
